//
//  shopuserMoneyListViewController.m
//  EasyToTesco
//
//  Created by 袖手旁觀 on 16/3/29.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "shopuserMoneyListViewController.h"
#import "AFNetworking.h"
#import "ShopUserMoneyListCell.h"
#import "ReadLoginData.h"
#import "FirstTableViewCell.h"

#define WEIGHT [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface shopuserMoneyListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,copy)NSString *userName;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *userDict;

@end

@implementation shopuserMoneyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSDictionary *dic = [ReadLoginData readLoginData];
    self.userName =[dic[@"data"] objectForKey:@"username"];
    
    [self loadUserData];
    
    [self addshopmoneytopviewUI];
    
    [self addTableView];
    
    
    
}




- (void)addTableView {
//    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WEIGHT, HEIGHT)];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.secondView.frame.size.height + 13, WEIGHT, HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 80;
}


#pragma mark -- dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"section%ld",self.userDict.count);
    return self.userDict.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (indexPath.section == 0) {
//    
//        FirstTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"FirstCell"];
//        
//        if (cell1 == nil) {
//            cell1 = [[[NSBundle mainBundle] loadNibNamed:@"FirstTableViewCell" owner:nil options:nil] lastObject];
//            
//            cell1.selectionStyle = UITableViewCellSelectionStyleNone;
//        }
//    }
    
        static NSString *ID = @"ShopUserMoneyListCell";
        ShopUserMoneyListCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ShopUserMoneyListCell" owner:nil options:nil] lastObject];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        NSLog(@"%ld",indexPath.row);
        /*
         UILabel *huiYuanMing;
         UILabel *dxh;
         UILabel *updatetime;
         UILabel *jine;
         */
        
        NSLog(@"%@",self.userDict[indexPath.section]);
        NSDictionary *userDataWithData = self.userDict[indexPath.section];
        cell.huiYuanMing.text = [NSString stringWithFormat:@"姓名:%@",[userDataWithData objectForKey:@"zhenming"]];
        cell.dxh.text = [NSString stringWithFormat:@"流水号:%@",[userDataWithData objectForKey:@"dxh"]];
        cell.updatetime.text = [NSString stringWithFormat:@"派送时间:%@",[userDataWithData objectForKey:@"addtime"]];
        cell.jine.text = [NSString stringWithFormat:@"派送金额:%@",[userDataWithData objectForKey:@"jine"]];
        
        return cell;
    }
    
//    return


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

//- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        return @"您好,欢迎您登入会员中心!";
//    }
//    return nil;
//}

#pragma mark -- setUpUI
- (void)addshopmoneytopviewUI {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"营业额结算";
    
}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma marlk -- 接口

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 80;
//}

- (NSMutableArray *)userDict {
    if (!_userDict) {
        _userDict = [NSMutableArray array];
    }
    return _userDict;
}

- (void)loadUserData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *param = @{@"username":self.userName};
    
    [manager POST:@"http://www.ylgw8.com/AppHz/pxjl" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
        self.userDict = [dict[@"data"] objectForKey:@"list"];
                NSLog(@"%@",[dict[@"data"] objectForKey:@"list"]);
//        NSLog(@"%@",self.userDict);
        NSLog(@"%d",self.userDict.count);
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}

@end
